//
//  KeyValueLabel.m
//  HuaRun2
//
//  Created by admin on 17/9/3.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "KeyValueLabel.h"

@interface KeyValueLabel ()

@end
@implementation KeyValueLabel

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    self.backgroundColor = kRandomColor;
    
    [self addSubview:self.keyLab];
    [self addSubview:self.valueLab];
    
}

- (void)layoutInit {
    weakself(ws);
    [self.keyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws).offset(kMargin10/ 2);
        make.left.mas_equalTo(ws).offset(0);
        make.width.mas_equalTo(85);
//        make.height.mas_equalTo(20);
//        make.right.mas_equalTo(ws.valueLab.mas_left).offset(-kMargin10);
    }];
    
    [self.valueLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws).offset(kMargin10 / 2);
        make.left.mas_equalTo(ws.keyLab.mas_right).offset(kMargin10 / 2);
        make.right.mas_equalTo(ws.mas_right).offset(-kMargin10/2);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(ws);
        make.bottom.mas_equalTo(ws.valueLab).offset(kMargin10/2);
    }];
}

- (void)setValueString:(NSString *)valueString {
    _valueString = valueString;
    
    BOOL isEmpty = [NSString isemptyStr:valueString];
    _valueLab.text = isEmpty ? @" " : valueString;
    
    if (isEmpty) {
        weakself(ws);
        [self mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(ws);
            make.height.mas_equalTo(30);
        }];
    }
}
- (UILabel *)keyLab {
    if (!_keyLab) {
        self.keyLab = [UILabel new];
        _keyLab.textAlignment = NSTextAlignmentRight;
        _keyLab.font = [UIFont boldSystemFontOfSize:12];
        _keyLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
        _keyLab.backgroundColor = kRandomColor;
    }
    return _keyLab;
}

- (UILabel *)valueLab {
    if (!_valueLab) {
        self.valueLab = [UILabel new];
        _valueLab.textAlignment = NSTextAlignmentLeft;
        _valueLab.font = kGeneralFontSize;
        _valueLab.textColor = [UIColor colorWithHex:@"3c3c3c"];

        _valueLab.numberOfLines = 0;
    }
    return _valueLab;
}

@end
